<?php

namespace app\admin\controller;

use app\admin\model\AuthExtend;
use app\admin\model\Config as ConfigModel;
use app\admin\model\Log as LogModel;
use app\admin\model\Member as MemberModel;
use app\admin\model\Menu as MenuModel;
use think\Controller;
use think\facade\Env;


class Admin extends Controller {

   // protected $middleware = ['auth'];


    protected function initialize() {

        parent::initialize();
        $config = cache('db_config_data');
        if (!$config) {
            $config = ConfigModel::listConfig();
            cache('db_config_data', $config);
        }
        config($config, 'app');

        defined('UID') or define('UID', is_login());
        if (!UID){
            $this->redirect(url('admin/publiz/login'));
        }
        defined('CID') or define('CID', session('user_auth.office_id'));
        defined('DID') or define('DID', 614);

//        // 检测系统权限
//        if (!is_admin()) {
//
//            $rule = strtolower($this->request->module() . '/' . $this->request->controller() . '/' . $this->request->action());
//            if (!$this->checkRule($rule)) {
//                $this->error('未授权访问!');
//            }
//        }

        $this->depositRules();
        $this->depositDatas();

        $this->assign('menus', MenuModel::getMenus('admin'));
        $this->assign('_admin_base_layout', config('admin_base_layout'));
        $this->assign('_theme_style', config('color_style'));

    }

    final protected function setMeta($meta_title = '') {
        $this->assign('meta_title', $meta_title);
    }

    protected function modifyStatus($controller_name = null) {
        parent::modifyStatus($controller_name);
    }

    final protected function log($record_id, $remark, $extra = null, $module = null, $model = null, $action = null) {
        if (empty($record_id)) {
            $this->error('数据ID不能为空');
        }

        $module = empty($module) ? $this->request->module() : $module;
        $model = empty($model) ? $this->request->controller() : $model;
        $action = empty($action) ? $this->request->action() : $action;

        $data = array();
        $data['module'] = strtolower($module);
        $data['model'] = strtolower($model);
        $data['action'] = strtolower($action);
        $data['record_id'] = $record_id;
        $data['remark'] = $remark;
        $data['extra'] = $extra;

        $data['action_ip'] = ip2long(get_client_ip());
        LogModel::create($data);
    }

    /**
     * action访问控制,在 **登陆成功** 后执行的第一项权限检测任务
     *
     * @return boolean|null  返回值必须使用 `===` 进行判断
     *
     *   返回 **false**, 不允许任何人访问(超管除外)
     *   返回 **true**, 允许任何管理员访问,无需执行节点权限检测
     *   返回 **null**, 需要继续执行节点权限检测决定是否允许访问
     * @author 艺品网络  <twothink.cn>
     */
    final protected function accessControl() {
        $allow = config('allow_visit');
        $deny = config('deny_visit');
        $check = strtolower($this->request->controller() . '/' . $this->request->action());
        if (!empty($deny) && in_array_case($check, $deny)) {
            return false; //非超管禁止访问deny中的方法
        }
        if (!empty($allow) && in_array_case($check, $allow)) {
            return true;
        }
        return null; //需要检测节点权限
    }

    /**
     * 权限检测
     * @param string $rule 检测的规则
     * @param string $mode check模式
     * @return boolean
     */
    final protected function checkRule($rule) {
        return \com\azthink\Auth::check($rule, UID, 'admin');
    }

    final protected function depositRules() {
        if (!is_admin()) {
            $action_rules = session('action_rules');
            if (!$action_rules) {
                $action_rules = MenuModel::getActionRules(UID, 'admin');
                session('action_rules', $action_rules);
            }
        }
    }

    final protected function depositDatas() {
        if (!is_admin()) {
            $data_rules = session('data_rules');
            if (!$data_rules) {
                $data_rules = AuthExtend::listAuthDatas(UID);
                session('data_rules', $data_rules['data_detail']);
            }
        }
    }

}